#!/bin/bash

#//////////////////////////////////////////////////////////////////////////////////
#
#  Betsu-Banner, OpenBSD Module. v0.1.5-exp - openbsd.bm | The Betsu Project
#
#//////////////////////////////////////////////////////////////////////////////////
#
#  Copyright (c) 2022, Alexa Hattori <unixcat@anche.no>.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
#  * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
#  * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#//////////////////////////////////////////////////////////////////////////////////
#
#  Module Authors:
#  Alexa Hattori <unixcat@anche.no>,
#  your_name <your_email>.
#
#//////////////////////////////////////////////////////////////////////////////////

BETSU_BANNER_VERSION="v0.1.5-exp"

# Temp (please delete later)
#custom_mountpoint="/dev"
###########################

print_battery () { # Battery percentage, + status, + remaining time
    echo "-" $(acpi | sed 's/Battery 0:/Battery is/')
}

print_cpu () {
    echo "- CPU Temp :" $(sysctl hw.sensors.cpu0.temp0 | sed 's/hw.sensors.cpu0.temp0=//' | sed 's/degC/C/')
}

print_lan_ip () { # Local LAN IP Address
    echo "- LAN IP :" $(hostname -I | sed 's/ .*//')
}

print_wan_ip4 () { # Public ipv4 Address
    echo "- WAN IP :" $(curl -s --ipv4 ident.me)
}

print_wan_ip6 () { # Public ipv6 Address
    echo "- WAN IP :" $(curl -s --ipv6 ident.me)
}

print_ram () { # RAM Usage
    echo "- RAM Usage :" $(free -m | awk '(NR==2){ print $3 }') "Mib" "/" $(free -m | awk '(NR==2){ print $2 }') "Mib"
}

print_swap () { # Swap Usage
    echo "- SWAP Usage :" $(free -m | awk '(NR==3){ print $3 }') "Mib" "/" $(free -m | awk '(NR==3){ print $2 }') "Mib"
}

print_storage_rootfs () { # rootfs ( / ) disk usage: used/total
    echo "- RootFS :" $(df -h / | awk '(NR==2){ print $3 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
         "/" $(df -h / | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')   
}

print_storage_userspace () { # userspace ( /home ) disk usage: used/total
    echo "- Userspace :" $(df -h /home | awk '(NR==2){ print $3 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
         "/" $(df -h /home | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')
}

print_storage_custom () {
    echo "- Custom Storage :" $(df -h $custom_mountpoint | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/') \
         "/" $(df -h $custom_mountpoint | awk '(NR==2){ print $2 }' | sed 's/G/ Gib/' | sed 's/M/ Mib/')
}

print_uptime () {
    echo "- Uptime :" $(uptime) 
}

# debug

debug () {
    print_battery
    print_cpu
    print_lan_ip
    print_wan_ip4
    print_wan_ip6
    print_ram
    print_swap
    print_storage_rootfs
    print_storage_userspace
    print_storage_custom
    print_uptime
}

debug
